/*
 * Copyright (c) 2001-2023 cnstrong.cn Corporation Limited. All rights reserved.
 * This software is the confidential and proprietary information of cnstrong.cn Company.
 * ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with cnstrong.cn.
 */
package com.treeconfig.common.base;

import java.io.Serializable;

/**
 * 通用分页入参
 *
 * @author Braveheart
 * @version V1.0
 * @since 2023-12-29
 */
public class CommonPageParam implements Serializable {
    private static final long serialVersionUID = 7241198347588013175L;

    /**
     * 默认每页显示的记录数
     */
    public static final int DEFAULT_PAGE_SIZE = 20;
    /**
     * 最大每页显示的记录数
     */
    public static final int MAX_PAGE_SIZE = 200;
    /**
     * 默认每页显示的记录数
     */
    public static final int DEFAULT_PAGE_NUMBER = 1;
    private int pageNumber;
    private int pageSize;
    private boolean lockedOffset;
    private int offset;

    public CommonPageParam() {
        this.pageSize = DEFAULT_PAGE_SIZE;
        this.pageNumber = DEFAULT_PAGE_NUMBER;
    }

    public CommonPageParam(int pageNumber, int pageSize) {
        if (pageNumber < 1) {
            throw new IllegalArgumentException("Page index must not be less than one!");
        }

        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than one!");
        }

        this.pageNumber = pageNumber;
        this.pageSize = pageSize > MAX_PAGE_SIZE ? MAX_PAGE_SIZE : pageSize;
    }

    public void lockOffset() {
        lockedOffset = true;
        this.offset = (pageNumber - 1) * pageSize;
    }

    public void unlockOffset() {
        lockedOffset = false;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getOffset() {
        if (lockedOffset) {
            return offset;
        }
        return (pageNumber - 1) * pageSize;
    }

    public void setPageNumber(int pageNumber) {
        if (pageNumber < 1) {
            throw new IllegalArgumentException("Page index must not be less than one!");
        }
        this.pageNumber = pageNumber;
    }

    public void setPageSize(int pageSize) {
        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than one!");
        }
        this.pageSize = pageSize > MAX_PAGE_SIZE ? DEFAULT_PAGE_SIZE : pageSize;
    }

    public String toString() {
        return "PageQuery{" +
            "pageNumber=" + pageNumber +
            ", pageSize=" + pageSize +
            '}';
    }
}
